package com.ysg.widget.dialog;

import android.content.Context;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.ysg.R;
import com.ysg.base.BaseDialog;
import com.ysg.utils.YCollectionUtil;
import com.ysg.utils.YRecyclerViewUtil;
import com.ysg.utils.YStringUtil;
import com.ysg.widget.dialog.adapter.DialogAdapter;
import com.ysg.widget.dialog.entity.DialogEntity;
import java.util.ArrayList;
import java.util.List;

/**
 * 列表
 */
public class ListDialog extends BaseDialog {
    public interface OnListListener{
        void onItemClick(DialogEntity entity);
    }
    private OnListListener listener;
    public void setOnListListener(OnListListener listener){
        this.listener = listener;
    }

    private String title;
    private TextView tvTitle;
    private RecyclerView recyclerView;
    private DialogAdapter adapter;
    private List<DialogEntity> list = new ArrayList<>();

    public ListDialog(Context context, String[] items) {
        super(context);
        for (String s: items) {
            list.add(new DialogEntity(s));
        }
    }

    public ListDialog(Context context, String title, String[] items) {
        super(context);
        this.title = title;
        for (String s: items) {
            list.add(new DialogEntity(s));
        }
    }

    public ListDialog(Context context, List<DialogEntity> list) {
        super(context);
        this.title = title;
        this.list = list;
    }

    public ListDialog(Context context, String title, List<DialogEntity> list) {
        super(context);
        this.title = title;
        this.list = list;
    }


    @Override
    public int getLayoutId() {
        return R.layout.dialog_list;
    }

    public void initView() {
        tvTitle = findViewById(R.id.tvTitle);
        recyclerView = findViewById(R.id.recyclerView);
        adapter = new DialogAdapter();
        YRecyclerViewUtil.init(mContext, recyclerView, adapter);
    }

    @Override
    public void initData() {
        if (YStringUtil.isNotEmpty(title)){
            tvTitle.setText(title);
        }
        if (YCollectionUtil.isNotEmpty(list)){
            adapter.setList(list);
        }

    }

    @Override
    public void initListener() {
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                dismiss();
                if (listener != null){
                    listener.onItemClick(list.get(position));
                }
            }
        });


    }


}
